package com.ocom.common.utils;

import java.util.ArrayList;
import java.util.List;

public class ObjectPool<T> {
    private final List<T> objects = new ArrayList<>();

    public T getObject() {
        synchronized (objects) {
            if(objects.size() > 0) {
                return objects.remove(0);
            } else {
                return createObject();
            }
        }
    }

    public void releaseObject(T object) {
        synchronized (objects) {
            objects.add(object);
        }
    }


    protected T createObject() {
        return null; // to be implemented by subclass
    }
}
